package com.xixi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xixi.entity.Order;
import com.xixi.entity.Product;
import com.xixi.mapper.OrderMapper;
import com.xixi.mapper.ProductMapper;
import com.xixi.mapper.UserMapper;
import com.xixi.pojo.dto.OrderDTO;
import com.xixi.pojo.query.OrderQueryPage;
import com.xixi.pojo.vo.OrderVO;
import com.xixi.pojo.vo.Result;
import com.xixi.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import static cn.hutool.core.util.RandomUtil.randomNumbers;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ProductMapper productMapper;
    @Override
    public IPage<OrderVO> page(OrderQueryPage queryPage) {
        IPage<OrderVO> page = new Page<>(queryPage.getPageNum(), queryPage.getPageSize());
        return orderMapper.page(page, queryPage);
    }

    @Override
    public OrderVO orderById(Integer id) {
        OrderVO orderVO = orderMapper.orderById(id);
        return orderVO;
    }
    @Transactional
    @Override
    public Result insert(OrderDTO orderDTO) {
        Order order = BeanUtil.toBean(orderDTO, Order.class);
        String orderNumber = generateOrderNumberWithSequence();
        order.setOrderNumber(orderNumber);
        order.setCreateTime(LocalDateTime.now());
        order.setUpdateTime(LocalDateTime.now());
        if(orderMapper.insert(order) > 0){
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    @Override
    public Result update(OrderDTO orderDTO) {
        Order order = BeanUtil.toBean(orderDTO, Order.class);
        order.setUpdateTime(LocalDateTime.now());
        if(orderMapper.updateById(order) > 0){
            return Result.success("修改成功");
        }
        return Result.error("修改失败");
    }

    @Override
    public Result delete(List<Integer> ids) {
        if(orderMapper.deleteBatchIds(ids) > 0){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }
    @Transactional
    @Override
    public Result buy(OrderDTO orderDTO) {
        Order order = BeanUtil.toBean(orderDTO, Order.class);
        String orderNumber = generateOrderNumberWithSequence();
        order.setOrderNumber(orderNumber);
        order.setCreateTime(LocalDateTime.now());
        order.setUpdateTime(LocalDateTime.now());
        Integer productId = order.getProductId();
        Product product=new Product();
        product.setStatus(4);
        product.setId(productId);
        if(orderMapper.insert(order) > 0 && productMapper.updateById(product) > 0){
            return Result.success("购买成功");
        }
        return Result.error("购买失败");
    }

    @Override
    public List<OrderVO> orderListById(Integer id) {
        List<OrderVO> list = orderMapper.orderListById(id);
        return list;
    }

    @Override
    public Result pay(List<Integer> ids) {
        if(orderMapper.pay(ids) > 0){
            return Result.success("支付成功");
        }
        return Result.error("支付失败");
    }

    @Override
    public Result receive(List<Integer> ids) {
        if(orderMapper.receive(ids) > 0){
            return Result.success("收货成功");
        }
        return Result.error("收货失败");
    }

    private String generateOrderNumberWithSequence() {
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String datePart = LocalDate.now().format(dateFormatter);
        String sequencePart = randomNumbers(3);
        return "ORDER" + datePart + sequencePart;
    }
}
